package ize.android;

import java.io.File;
import java.io.FilenameFilter;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SlideshowWallpaper extends WallpaperService {
	public static final String SETTINGS = "slideshowwallpapersettings";
	

	@Override
    public void onCreate() {
        super.onCreate();
        
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    

    @Override
    public Engine onCreateEngine() {
        return new SlideshowEngine();
    }

    class SlideshowEngine extends Engine
    {

    	private SlideshowWallpaperThread slideshowWallpaperThread;
        
        
    	
    	public SlideshowEngine() {
    		//constructor    	
    		
    		SharedPreferences prefs = getSharedPreferences(SlideshowWallpaper.SETTINGS, 0);
    		SurfaceHolder holder = getSurfaceHolder();
    		Context context = getApplicationContext();
    		
    		
    		slideshowWallpaperThread = new SlideshowWallpaperThread(prefs, holder, context);
    	   	 
		}
    	
    	
    	 	    	
    	
    	@Override
    	public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);

            // By default we don't get touch events, disable them anyway
            setTouchEventsEnabled(false);
            
            
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            slideshowWallpaperThread.stopDrawing();
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
             
             if (visible) {
                 // register listeners and callbacks here
            	 slideshowWallpaperThread.resumeDrawing();
             } else {
                 // remove listeners and callbacks here
            	 slideshowWallpaperThread.pauseDrawing();
             }
        }
        
        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            // store the center of the surface, so we can draw the cube in the right spot
            
            slideshowWallpaperThread.setSurfaceSize(width, height);
            
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            slideshowWallpaperThread.start();
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            boolean retry = true;
            slideshowWallpaperThread.stopDrawing();
            while (retry) {
                try {
                	slideshowWallpaperThread.join();
                    retry = false;
                } catch (InterruptedException e) {}
            }
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xStep, float yStep, int xPixels, int yPixels) {
        }

        /*
         * Store the position of the touch event so we can use it for drawing later
         */
        @Override
        public void onTouchEvent(MotionEvent event) {

            super.onTouchEvent(event);
        }
    	
        
    }
}
